package com.conversationboard.model;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;


public class NameTruncation {

	public static String truncateName(String displayName, String originalName) {

		if ((originalName == null) || originalName.trim().equals("")) {
			return displayName;
		}

		originalName = originalName.trim();

		if (displayName.equals(originalName)) {
			return displayName;
		}

		String originalNameWithBrackets = " (" + originalName + ")";

		int totalLength = displayName.length() + originalNameWithBrackets.length();

		if (totalLength <= 80) {
			return displayName + originalNameWithBrackets;
		}

		int extra = (totalLength - 80);
		int x = Math.min(80, displayName.length());
		int truncateTo = (x - extra);

		if (truncateTo < 0) {
			return originalName;
		}

		if (truncateTo > displayName.length()) {
			return originalName;
		}

		String truncatedDisplayName = displayName.substring(0, truncateTo);

		String fullDisplayName = truncatedDisplayName + originalNameWithBrackets;

		return fullDisplayName;
	}

	
	public static void main(String[] args) throws Exception {

		List<String> lines = FileUtils.readLines(new File("/tmp/keith.csv"));

		for (String line : lines) {
			
			String[] parts = line.split(",");
			
			String name = NameTruncation.truncateName(parts[0], parts[1]);
			
			System.out.println(name);
			
			
		}
		
		
	}
	
	
}
